/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jims.api.v1.controller.sync;
import com.jims.master.common.utils.Encodes;
import com.jims.master.common.utils.SecurityUtil;

import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.base.user.entity.MasterBaseinfo;
import com.jims.master.modules.base.user.service.MasterBaseinfoService;
import com.jims.master.modules.sync.entity.*;
import com.jims.master.modules.sync.service.*;
import com.jims.master.modules.weChatSweepPay.pay.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baomidou.kisso.my.controller.BaseController;
import com.jims.master.common.data.Message;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 住院记录Controller
 * @author xueyx
 * @version 2017-11-16
 */
@RestController
@RequestMapping(value = "patvisit")
public class PatVisitController extends BaseController {

	@Autowired
	private PatVisitService patVisitService;
    @Autowired
    private PatVisitDetailService patVisitDetailService;
    @Autowired
    private DiagnosisService diagnosisService;
    @Autowired
    private HfOperationRecService hfOperationRecService;
    @Autowired
    private MasterBaseinfoService masterBaseinfoService;
    @Autowired
    private CostService costService;

    /***
     * 病案首页
     * @param visitId
     * @return
     */
    @RequestMapping(value = "/V100/cusinfo",method ={RequestMethod.GET,RequestMethod.POST})
    public String cusinfo(String visitId) {
        Map<String, Object> map = new HashMap<String, Object>();
        PatVisit patVisit = new PatVisit();
        patVisit.setId(visitId);
        patVisit = patVisitService.get(patVisit);//加载病人信息
        patVisit.setIsAgreeAutopsy("");
        //Cost cost = costService.getByVisitId(visitId);
        MasterBaseinfo patientBasicInfo = new MasterBaseinfo();
        if(patVisit.getIdCard()!=null)
            patientBasicInfo.setIdCard(patVisit.getIdCard());
            patientBasicInfo = masterBaseinfoService.getByIdCard(patientBasicInfo);
        map.put("patientBasicInfo",patientBasicInfo);
        map.put("patVisit",patVisit);
       /* map.put("cost",cost);*/
        PatVisitDetail patVisitDetail = new PatVisitDetail();
        patVisitDetail.setVisitId(visitId);
        patVisitDetail =patVisitDetailService.getByVisitId(patVisitDetail);
        map.put("patVisitDetail",patVisitDetail);
        //查询门诊诊断
        Diagnosis diagnosis = new Diagnosis();
        diagnosis.setVisitId(visitId);
        diagnosis.setState("10");
        List<Diagnosis> diagnosisClinicList = diagnosisService.findList(diagnosis);
        if(diagnosisClinicList.size()>0){
            map.put("diagnosisClinic", diagnosisClinicList.get(0));
        }else{
            map.put("diagnosisClinic", null);
        }
        //查询出院诊断
        Diagnosis diagnosisOut = new Diagnosis();
        diagnosis.setVisitId(visitId);
        diagnosis.setState("5");
        /*List<Diagnosis> diagnosisOutList= diagnosisService.findList(diagnosisOut);
        int num =0;
        int size=diagnosisOutList.size();
        while(size+num<22){
            Diagnosis add = new Diagnosis();
            add.setIcdName(null);
            add.setDiagnosisId("------");
            add.setSituationInStr("------");
            diagnosisOutList.add(add);
            num=num+1;
        }
        map.put("diagnosisOutList", diagnosisOutList);*/
        //查询损伤中毒原因
        Diagnosis diagnosisPoison = new Diagnosis();
        diagnosisPoison.setVisitId(visitId);
        diagnosisPoison.setState("8");
        List<Diagnosis> diagnosisPoisonList= diagnosisService.findList(diagnosisPoison);
        if(diagnosisPoisonList!=null && diagnosisPoisonList.size()>0){
            map.put("diagnosisPoison", diagnosisPoisonList.get(0));
        }
        else{
            map.put("diagnosisPoison", null);
        }
        //查询病理诊断
        Diagnosis diagnosisPathology = new Diagnosis();
        diagnosisPathology.setState("7");
        List<Diagnosis> diagnosisPathologyList= diagnosisService.findPathologyList(visitId);
        if(diagnosisPathologyList!=null && diagnosisPathologyList.size()>0){
            map.put("diagnosisPathology", diagnosisPathologyList.get(0));
        }
        else{
            map.put("diagnosisPathology", null);
        }

        //查询病人手术信息,目前只显示住院手术，如果以后有门诊手术显示逻辑在考虑
        HfOperationRec operation =new HfOperationRec();
        operation.setVisitId(visitId);
        List<HfOperationRec>  operationList =hfOperationRecService.findList(operation);
        map.put("operationList", operationList);


        //查询病人费用信息
        Message mes = new Message();
        mes.setData(map);
        return SecurityUtil.encryptMsgData(mes);
    }

    @RequestMapping(value = "/V100/getInflag",method ={RequestMethod.GET,RequestMethod.POST})
    public String getInflag(String visitId) {
        PatVisit patVisit = patVisitService.get(visitId);
        String inflag ;
        if(patVisit.getDischargeDate()!=null){
            inflag = "1";
        }else {
            inflag = "0";
        }
        Message mes = new Message();
        mes.setData(inflag);
        return SecurityUtil.encryptMsgData(mes);
    }

    /*
     *入院记录
     * @param visitId
     * @return
     */
    @RequestMapping(value = "/V100/getInfo",method ={RequestMethod.GET,RequestMethod.POST})
        public String getInfo(String visitId) {
        Map<String, Object> map = new HashMap<String, Object>();
        PatVisit patVisit = new PatVisit();
        patVisit.setId(visitId);
        patVisit = patVisitService.get(patVisit);//加载病人信息
        if(StringUtils.isNotBlank(patVisit.getInDiagnosisId())){
            List<Diagnosis> diagnosisInList= diagnosisService.findEmrDiagnosisList(patVisit.getInDiagnosisId());
            map.put("diagnosisInList", diagnosisInList);
        }
        //主訴
        patVisit.setActionInChief(Encodes.unescapeHtml(patVisit.getActionInChief()));
        //现病史
        patVisit.setPresentHistory(Encodes.unescapeHtml(patVisit.getPresentHistory()));
        //既往史
        //各人史
        patVisit.setPersonalHistory(Encodes.unescapeHtml(patVisit.getPersonalHistory()));
        //婚育史
        patVisit.setMarriageHistory(Encodes.unescapeHtml(patVisit.getMarriageHistory()));
        //家族史
        patVisit.setFamilyHistory(Encodes.unescapeHtml(patVisit.getFamilyHistory()));
        //辅助检査结果
        patVisit.setAccessoryExam(Encodes.unescapeHtml(patVisit.getAccessoryExam()));
        map.put("patVisit", patVisit);
        Message mes = new Message();
        mes.setData(map);
        return SecurityUtil.encryptMsgData(mes);
    }

    /**
     * @Title: ordersList
     * @Description: 住院详情
     * @param
     * @author WangYQ
     * @date 2018/10/09
     * @throws
     */
    @RequestMapping(value = "V100/patVisitInfoOfHealth", method = {RequestMethod.GET, RequestMethod.POST})
    public String patVisitInfoOfHealth(String url)throws UnsupportedEncodingException {
        url = URLDecoder.decode(url, "utf-8");
        url = url.replace("&amp;", "&");
        String s = HttpUtil.postData(url,"");
        return s;
    }
}